package com.guigu.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.guigu.health.pojo.*;
import com.guigu.health.service.CheckGroupService;
import com.guigu.health.service.CheckItemService;
import com.mysql.jdbc.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName:CheckGroupController
 * @Description:检查组控制层
 * @Author:longtao
 * @Date:2021/4/15
 * @Version:1.0
 */

@RequestMapping("/checkGroup")
@Controller
public class CheckGroupController extends BaseController{

    @Reference
    private CheckGroupService checkGroupService;

    @Reference
    private CheckItemService checkItemService;

    /**
     * 模糊查询检查组并分页
     * @param name
     * @param pageNum
     * @return
     */
    @RequiresPermissions(value = {"CHECKGROUP_QUERY"})
    @ResponseBody
    @RequestMapping(value = "/showCheckGroup",method = RequestMethod.GET,produces = {"application/json;charset=utf-8"})
    public Object showCheckGroup(String name,String pageNum){
        if (StringUtils.isNullOrEmpty(pageNum)){
            pageNum="1";
        }
        PageInfo<CheckGroupCheckItem> pageInfo = checkGroupService.queryAllList(name, Integer.valueOf(pageNum), 2);
        return pageInfo;
    }

    /**
     * 删除检查组
     * @param id
     * @return
     */
    @RequiresPermissions(value = {"CHECKGROUP_DELETE"})
    @ResponseBody
    @RequestMapping(value = "/deleteCheckGroup")
    public Object deleteCheckGroup(String id){
        Object b=null;
        try {
            b=checkGroupService.deleteCheckGroup(Integer.valueOf(id));
        }catch (Exception e){
            b="-997";
            e.printStackTrace();
            return b;
        }
        return b;
    }

    /**
     *新增检查组
     * @param map
     * @return
     */
    @RequiresPermissions(value = {"CHECKGROUP_ADD"})
    @ResponseBody
    @RequestMapping(value = "/addCheckGroup")
    public Object addCheckGroup(@RequestBody Map<String,Object> map){
        boolean b=false;
        String checkGroup = String.valueOf(map.get("checkGroup"));
        String checkList = String.valueOf(map.get("checkList"));
        System.out.println("checkGroup = " + checkGroup);
        System.out.println("checkList = " + checkList);
        CheckGroup checkGroup2 = JSONObject.parseObject(checkGroup, CheckGroup.class);
        List<String> list = JSONObject.parseArray(checkList, String.class);
        System.out.println("checkGroup2 = " + checkGroup2);
        System.out.println("list = " + list);
        b=checkGroupService.addCheckGroup(checkGroup2);
        List<CheckGroup> all = checkGroupService.queryAll();
        List<CheckGroup_CheckItemKey> list1=new ArrayList<CheckGroup_CheckItemKey>();
        for (String s : list) {
           if (!StringUtils.isNullOrEmpty(s)){
               CheckGroup_CheckItemKey checkItemKey=new CheckGroup_CheckItemKey();
               checkItemKey.setCheckgroup_id(all.get(all.size()-1).getId());
               checkItemKey.setCheckitem_id(Integer.valueOf(s));
               list1.add(checkItemKey);
           }
        }
        for (CheckGroup_CheckItemKey key : list1) {
            System.out.println("key = " + key);
        }
        b=checkGroupService.batchAdd(list1);
        return b;
    }

    /**
     * 修改检查组
     * @param checkGroup
     * @return
     */
    @RequiresPermissions(value = {"CHECKGROUP_EDIT"})
    @ResponseBody
    @RequestMapping(value = "/updateCheckGroup")
    public Object updateCheckGroup(@RequestBody CheckGroup checkGroup,int [] checkItemIds){
        CheckGroupExample example=new CheckGroupExample();
        CheckGroupExample.Criteria criteria=example.createCriteria();
        criteria.andIdEqualTo(Integer.valueOf(checkGroup.getId()));
        boolean b = checkGroupService.updateCheckGroup(checkGroup, example,checkItemIds);
        return b;
    }
    @RequiresPermissions(value = {"CHECKGROUP_QUERY"})
    @ResponseBody
    @RequestMapping(value = "/showCheckItem")
    public Object showCheckItem(){
        List<CheckItem> list = checkItemService.querAll();
        return list;
    }


    @RequiresPermissions(value = {"CHECKGROUP_QUERY"})
    @ResponseBody
    @RequestMapping(value = "/showCheckItemById")
    public Object showCheckItemById(String id){
        List<Integer> list = checkGroupService.getCountCheckItemIds(Integer.valueOf(id));
        return list;
    }

    /**
     * 判断用户名一致
     * @param code
     * @return
     */
    @RequiresPermissions(value ={"CHECKGROUP_QUERY"})
    @ResponseBody
    @RequestMapping(value = "/count")
    public Object count(String code){
        boolean flag=true;
        int i=checkGroupService.count(code);
        if (i>0){
            flag=false;
        }
        return flag;
    }
}
